<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/10
 * Time: 20:05
 */

defined('BASEPATH') OR exit('No direct script access allowed');

class ShopifyGoods extends Admin_Controller
{
    public function index()
    {
        $this->load->model('shopify_goods_model');
        $this->load->model('order_model');
        $this->load->model('siteweb_model');

        $this->load->library('pagination');

        $assoc = $this->uri->uri_to_assoc();

        $siteweb = $this->siteweb_model->siteweb_option();

        $_param_option = array(
            'goods_name' => array(
                'col' => 'col-lg-4',
                'type' => 'text',
                'placeholder' => '输入产品名称、货号'
            ),
            'siteweb' => array(
                'col' => 'col-lg-2',
                'type' => 'option',
                'placeholder' => '选择站点',
                'option' => $siteweb
            )
        );

        $_param = array(
            'goods_name' => $this->input->get('goods_name'),
            'siteweb' => $this->input->get('siteweb'),
            'page' => $assoc['page'] > 0 ? intval($assoc['page']) - 1 : 0
        );

        $config['base_url'] = base_url('ShopifyGoods/') . 'index/page/';
        $config['total_rows'] = $this->shopify_goods_model->count($_param);
        $config['num_links'] = 6;
        $config['per_page'] = $this->shopify_goods_model->limit_rows;
        $config['use_page_numbers']=true;
        $this->pagination->initialize($config);

        $_goods_list = $this->shopify_goods_model->goods_list($_param);

        foreach ($_goods_list as $key => $row)
        {

            $_goods_list[$key]['goods_num'] = $this->order_model->order_goods_num($row['shopify_id']);

        }


        if($_param['siteweb'] > 0){
            $start = strtotime(date('Y-m'));
            $end = strtotime(date('Y-m-d'));

            $maichu = $this->db->from('order_goods og')
            ->select('goods_id')
            ->join('order_info as oi', 'og.order_id = oi.order_id', 'left')->where(['oi.web_id'=>$_param['siteweb']])
            ->where("oi.add_time between $start and $end")
            ->group_by('goods_id')
            ->get()->result_array();
            $mai_goods = count($maichu);
            $goods_count = $this->db->from('goods')
            ->select('count(*) goods_count')
            ->where("add_time between $start and $end")
            ->get()->result_array();

            if($goods_count[0]['goods_count'] == 0){
                $res = $mai_goods . '/' . '0 = 0 %';
            }else{
                $res = $mai_goods. '/' . $goods_count[0]['goods_count'] . '='. $mai_goods / $goods_count[0]['goods_count'] * 100 .'%';
            }
            $data['res'] = $res;
        }
        

        $data['goods_list'] = $_goods_list;
        $data['pages'] = $this->pagination->create_links();
        $data['site_option'] = $this->siteweb_model->siteweb_option();
        $data['param_option'] = $_param_option;
        $data['siteweb'] = $siteweb;
        $data['param'] = $_param;

        
        $this->load->view('shopify_goods_list', $data);
    }
    
    public function dong_xiao_lv(){
        $this->load->model('siteweb_model');
        $siteweb = $this->siteweb_model->siteweb_option();
        $this->load->model('category_model');
        $cat_one = $this->category_model->cat_one();
        $this->load->model('admin_model');
        $admin_name = $this->admin_model->admin_option();
        $_param_option = array(
            'goods_time' => array(
                'col' => 'col-lg-4',
                'type' => 'dpd_time',
                'placeholder' => '上传时间'
            ),
            'siteweb' => array(
                'col' => 'col-lg-2',
                'type' => 'option',
                'placeholder' => '选择站点',
                'option' => $siteweb
            ),
            'cate_name' => array(
                'col' => 'col-lg-2',
                'type' => 'option_cat',
                'placeholder' => '选择分类',
                'option' => $cat_one
            ),
            'admin_id' => array(
                'col' => 'col-lg-2',
                'type' => 'option',
                'placeholder' => '请选择开发员',
                'option' =>$admin_name
            ),
        );

        $_param = array(
            'siteweb' => $this->input->get('siteweb'),
            'goods_time_start' => $this->input->get('goods_time_start'),
            'goods_time_end' => $this->input->get('goods_time_end'),
            'cate_name' => $this->input->get('cate_name'),
            'admin_id' => $this->input->get('admin_id')
        );

        



        //开发商品
        if($_param['cate_name'] != '选择分类'){
            $this->db->where(['g.cat_id'=>$_param['cate_name']]);
        }
        if($_param['admin_id'] > 0){
            $this->db->where(['g.admin_id'=>$_param['admin_id']]);
        }
        if($_param['siteweb'] > 0){
            $this->db->where(['sg.web_id'=>$_param['siteweb']]);
        }
        if($_param['goods_time_start'] && $_param['goods_time_end']){
            $goods_time_start = strtotime($_param['goods_time_start']);
            $goods_time_end = strtotime($_param['goods_time_end']);
            $this->db->where("g.add_time between $goods_time_start and $goods_time_end");
        }else{
            $start = strtotime(date('Y-m'));
            $end = strtotime(date('Y-m-d'));
            $this->db->where("g.add_time between $start and $end");
        }
        $goods_id = $this->db->from('shopify_goods sg')
        ->select('g.goods_id')
        ->group_by('g.goods_id')
        ->join('goods as g', 'sg.goods_id = g.goods_id', 'left')
        ->get()->result_array();

        foreach ($goods_id as $k => $v) {
            $arr[] = $v['goods_id'];
        }
        if($arr){
            $goods_count = count($arr);

            //卖出商品
            if($_param['siteweb'] > 0){
                $this->db->where(['oi.web_id'=>$_param['siteweb']]);
            }
            if($_param['admin_id'] > 0){
                $this->db->where(['g.admin_id'=>$_param['admin_id']]);
            }
            if($arr){
                $this->db->where_in('og.goods_id',$arr);
            }
            if($_param['goods_time_start'] && $_param['goods_time_end']){
                $goods_time_start = strtotime($_param['goods_time_start']);
                $goods_time_end = strtotime($_param['goods_time_end']);
                $this->db->where("oi.add_time between $goods_time_start and $goods_time_end");
            }else{
                $start = strtotime(date('Y-m'));
                $end = strtotime(date('Y-m-d'));
                $this->db->where("oi.add_time between $start and $end");
            }

            if($_param['cate_name'] != '选择分类'){
                $this->db->where(['g.cat_id'=>$_param['cate_name']]);
            }

            $maichu = $this->db->from('order_goods og')
                    ->select('og.goods_id')
                    ->join('order_info as oi', 'og.order_id = oi.order_id', 'left')
                    ->join('goods as g','g.goods_id = og.goods_id','left')
                    ->group_by('og.goods_id')
                    ->get()->result_array();
            $mai_goods = count($maichu);
        }else{
            $mai_goods = 0;
        }
        







        if($goods_count == 0){
            $res = $mai_goods . '/' . '0 = 0 %';
        }else{
            $res = $mai_goods. '/' . $goods_count . '='. $mai_goods / $goods_count * 100 .'%';
        }
        // echo $res;die;
        $data['param_option'] = $_param_option;
        $data['res'] = $res;
        $data['param'] = $_param;
        $this->load->view('dong_xiao_lv', $data);
    }
    public function set()
    {
        $this->load->model('shopify_goods_model');
        $this->load->model('goods_model');
        $this->load->model('siteweb_model');
        $this->load->model('tagslist_model');
        $assoc = $this->uri->uri_to_assoc();
        $this->load->library('sys_message');

        $sg_id = intval($assoc['sg_id']);
        
        $shoify_goods = $this->shopify_goods_model->goods_info(array('sg_id' => $sg_id));
        
        !$shoify_goods && $this->sys_message->wrong('产品不存在！');
        
        $web_info = $this->siteweb_model->siteweb_info(array('web_id' => $shoify_goods['web_id']), 1);
    
        $_variants = unserialize($shoify_goods['variants']);
    
        $variants = array();
        foreach ($_variants as $v) {
            $sku = preg_replace("/[a-zA-Z]+/i", '', $v['sku']);
            $variants[$sku] = $v;
        }
        
        $goods = $this->goods_model->goods_info($shoify_goods['goods_id'], 1);
        
        if ($_POST) {
            
            $data = array(
                'goods_name' => trim($this->input->post('goods_name')),
                'market_price' => sprintf('%.2f', $this->input->post('market_price')),
                'shop_price' => sprintf('%.2f', $this->input->post('shop_price')),
                'cate_name' => trim($this->input->post('cate_name')),
                'is_sales' => intval($this->input->post('is_sales')),
                'variants' => array(),
                //'tags' => '',
                'body_html' => trim($this->input->post('summernote'))
            );
            
            !$data['goods_name'] && $this->sys_message->wrong('请填写产品名称！');
            !$data['shop_price'] && $this->sys_message->wrong('请填写产品售价！');
            !$data['body_html'] && $this->sys_message->wrong('请填写产品描述！');
            
            $post_sku = $this->input->post('sku');
            if(empty($post_sku)){
                $this->sys_message->wrong('请添加SKU！');
            }
            foreach ($post_sku as $num_sn) {
                $_goods_sku = $goods['sku'][$num_sn];
                if ($_goods_sku) {
                    $data['variants'][] = array(
                        'option1' => $_goods_sku['size'],
                        'option2' => $_goods_sku['color'],
                        'sku' => $web_info['ab_name'] . $num_sn,
                        'price' => $data['shop_price']
                    );
                }
            }
            !$data['variants'] && $this->sys_message->wrong('请添加SKU！');
            
    
            $post_tags = $this->input->post('tags');
            if ($post_tags) {
                foreach ($post_tags as $k => $tag) {
                    if ($tag) {
                        $post_tags[$k] = trim($tag);
                    }
                }
                $data['tags'] = implode(',', $post_tags);
            }
            $data['tags'] = $this->tagslist_model->tags_in($data['tags']);
            $this->shopify_goods_model->insert_goods($data, $sg_id);
            //上传站点
            $shopify_data = $this->shopify_goods_model->up_shopify_data($sg_id);
            $siteweb = $this->siteweb_model->siteweb_info(array('web_id' => $shopify_data['web_id']));

            //查看goods表 shopify_id不存在就是老商品
            $res = $this->db->from('goods')->where(['shopify_id'=>$shoify_goods['shopify_id']])->get();
            if($res->row_array()){
                $shopify_data['cunzai'] = 1;
            }else{
                $shopify_data['cunzai'] = 0;
            }
            
            $this->load->library('shopify_api', $siteweb);
            $response = $this->shopify_api->update_up_goods($shopify_data);
            $this->sys_message->success('成功!');
        }
        $shoify_goods['tags'] = explode(',',$shoify_goods['tags']);
        $tags = $this->tagslist_model->tags_find();
        foreach ($tags as $key => $value) {
            foreach ($shoify_goods['tags'] as $k => $v) {
                if($value['tags_name'] == $v){
                    $tags[$key]['che'] = 1;
                }
            }
        }
        $data = array(
            'tags'=>$tags,
            'shopify_goods' => $shoify_goods,
            'variants' => $variants,
            'goods' => $goods,
            'web_info' => $web_info
        );
        
        $this->load->view('shopify_goods_set', $data);
    
    }

    //批量上传页面
    public function batch_upload(){

        if($_POST){
            $this->load->model('siteweb_model');
            $this->load->model('goods_model');
            $this->load->model('shopify_goods_model');
            $this->load->model('goods_sku_model');
            $this->load->library('sys_message');
            $this->load->library('shopify_api');
            $this->load->model('tagslist_model');
            //商品编号切割 查出商品信息
            $goods_sn = $this->input->post('goods_sn');
            $goods_sn = explode(",",preg_replace("/[^a-zA-Z0-9,]/u","", str_replace(array("\r\n", "\r", "\n"), ",", $goods_sn)));
            if(empty($goods_sn)){
                $this->sys_message->success('商品查不到!');
            }
            $query = $this->db->from('goods')->where_in('goods_sn',$goods_sn)->get();
            $goods_res = $query->result_array();
            //站点切割
            $text     = $this->input->post('text');
            //$texts = explode(',',$text); 
            foreach ($text as $k => $web_id) {
                foreach ($goods_res as $key => $goods) {
                    sleep(1);
                    //查询站点
                    $siteweb = $this->siteweb_model->siteweb_info(array('web_id' => $web_id));
                    if (!$siteweb) {
                        continue;
                    }
                    //查询商品是否已经上传，上传过跳出本次循环
                    $arr = array('goods_id' => $goods['goods_id'], 'web_id' => $web_id);
                    $shopify_goods = $this->shopify_goods_model->goods_info($arr);
                    if ($shopify_goods) {
                        continue;
                    }
                    //查询商品sku 下架查不到跳出
                    $goods_sku = $this->goods_model->get_goods_sku($goods['goods_id'], 1);
                    if (empty($goods_sku)) {
                        continue;
                    }

                    $variants = array();
                    foreach ($goods_sku as $row) {
                        $variants[] = array(
                            'option1' => $row['size'],
                            'option2' => $row['color'],
                            'sku' => $siteweb['ab_name'] . $row['sku_num_sn'],
                            'price' => $goods['shop_price']
                        );
                    }
                    $goods['goods_attr'] = json_decode($goods['goods_attr'], true);
                    if ($goods['goods_attr'] && count($goods['goods_attr']['attr_val']) > 1) {
                        $html = '<table><tr>';
                        foreach ($goods['goods_attr']['attr_title'] as $attr_title) {
                            $html .= '<td>' . $attr_title . '</td>';
                        }
                        $html .= '</tr>';

                        $_attr = array();
                        foreach ($goods['goods_attr']['attr_val'] as $k => $_row) {
                            foreach ($_row as $_k => $_row_attr) {
                                $_attr[$_k][$k] = $_row_attr;
                            }
                        }

                        foreach ($_attr as $k_val => $_attr_val) {
                            $html .= '<tr>';
                            foreach ($_attr_val as $k_title => $attr_title) {
                                if ($k_title == 0) {
                                    $html .= '<td>' . $attr_title . '</td>';
                                } else {
                                    $attr_val = explode('-', $attr_title);

                                    $a1 = sprintf('%.1f', $attr_val[0] / 2.54);
                                    $a2 = $attr_val[1] ? ("-" . sprintf('%.1f', $attr_val[1] / 2.54)) : '';
                                    $html .= '<td>' . $a1 . $a2 . '(' . $attr_title . 'cm)' . '</td>';
                                }
                            }
                            $html .= '</tr>';
                        }

                        $html .= '</table>';
                    }

                    $data = array(
                        'is_batch' => 1,
                        'goods_id' => $goods['goods_id'],
                        'goods_sn' => $siteweb['ab_name'] . preg_replace("/[a-zA-Z]/","",$goods['goods_sn']),
                        'web_id' => $web_id,
                        'goods_name' => $goods['goods_name'],
                        'cat_id' => $goods['cat_id'],
                        'shop_price' => $goods['shop_price'],
                        'market_price' => $goods['market_price'],
                        'body_html' => $goods['body_html'] . $html,
                        'is_sales' => 1,
                        'variants' => $variants,
                        'tags' => $this->tagslist_model->tags_in($goods['tags'])
                    );
                    $this->shopify_goods_model->insert_goods($data);
                }
            }
            $this->sys_message->success('成功!');
        }else{
            $query = $this->db->from('siteweb')->where(['is_delete'=>0])->get();
            $res = $query->result_array();
            $data = array(
                'res' => $res,
            );
            $this->load->view('batch_upload',$data);
        }
        
    }

    /**
     * 批量上传到ueeshop
     * batch_ueeshop_up
     * author: walker
     * Date: 2019/12/5
     * Time: 10:38
     * Note:
     */
    public function batch_ueeshop_up()
    {

        $this->load->model('siteweb_model');
        $this->load->model('shopify_goods_model');
        $this->load->model('goods_model');

        $web_list = $this->siteweb_model->siteweb_list(array(
            'type' => 3
        ));

        $web_cate = array();
        foreach ($web_list as $row) {
            if ($row['cate']) {
                $web_cate['_'.$row['web_id']] = json_decode($row['cate'], true);
            }
        }

        $data = array(
            'web_list' => $web_list,
            'web_cate' => $web_cate,
        );

        $this->load->view('batch_ueeshop_up', $data);
    }

    //批量同步更新
    public function update_all(){
        if($_POST){
            $this->load->library('sys_message');
            $this->load->model('shopify_goods_model');
            $this->load->model('siteweb_model');
            $this->load->model('goods_model');
            $this->load->library('shopify_api');
            $this->load->model('tagslist_model');
            $goods_sn = $this->input->post('goods_sn');
            $web_id = $this->input->post('web_id');
            $arr = [];
            foreach (explode(',',$goods_sn) as $key => $val) {
                foreach ($web_id as $k => $v) {
                    $arr[] = ['goods_sn'=>$val,'web_id'=>$v];
                }
            }
            // print_R($arr);die;
            foreach ($arr as $key => $value) {
                sleep(1);
                $goods_id = $this->db->from('goods')->where(['goods_sn'=>$value['goods_sn']])->get()->row_array();
                if(empty($goods_id)){
                    $this->sys_message->wrong('商品不存在!');
                }
                $info = $this->db->from('shopify_goods')
                        ->where(['goods_id'=>$goods_id['goods_id'],'web_id'=>$value['web_id']])
                        ->get()->row_array();
                if(!empty($info)){
                    $goods_id = $this->db->from('goods')
                            ->select('goods_id')
                            ->where(['goods_sn'=>$value['goods_sn']])
                            ->get()->row_array();
                    $value['goods_id'] = $goods_id['goods_id'];
                    
                    $is = $this->db->from('goods_upload')
                        ->where(['goods_id'=>$value['goods_id'],'web_id'=>$value['web_id'],'is_upload'=>0])
                        ->get()->row_array();
                        // print_R($is);die;
                    if($is){
                        continue;
                    }else{
                        $this->db->insert('goods_upload',$value);
                    }
                }else{
                    $this->sys_message->wrong('商品没上传过站点!');
                }
            }
            $this->sys_message->success('成功!');
            
        }
        $query = $this->db->from('siteweb')->where(['is_delete'=>0])->get();
        $res = $query->result_array();
        $data = array(
            'res' => $res,
        );
        $this->load->view('update_all',$data);
    }

    public function goods_up_web_all(){
        $this->load->library('sys_message');
        $this->load->model('shopify_goods_model');
        $this->load->model('siteweb_model');
        $this->load->model('goods_model');
        $this->load->library('shopify_api');
        $this->load->model('tagslist_model');

        $res = $this->db->from('goods_upload')->where(['is_upload'=>0])->limit(5)->get()->result_array();
        if(empty($res)){
            return false;
        }
        foreach ($res as $k => $v) {
            sleep(3);
            $goods = $this->goods_model->goods_info($v['goods_id']);


            $siteweb = $this->db->from('siteweb as s')
            ->select('s.*,sg.sg_id,sg.shopify_id, sg.is_sales')
            ->join('shopify_goods as sg', 's.web_id = sg.web_id', 'left')
            ->where(['sg.goods_id'=>$v['goods_id'],'s.web_id'=>$v['web_id']])
            ->get()->row_array();

// print_R($goods['goods_id']);die;

            $images_query = $this->db->from('goods_album')->where(['goods_id'=>$goods['goods_id'],'is_delete'=>0])->get();
            $goods['images'] = array();
            foreach ($images_query->result_array() AS $row) {
                if (strpos($row['original_img'],'https://') !== false) {
                    $goods['images'][] = array(
                        'src' => $row['original_img']
                    );
                } else {
                    $goods['images'][] = array(
                        'attachment' => $this->shopify_goods_model->imgToBase64($row['original_img'])
                    );
                }

            }

            $goods_sku = $this->goods_model->get_goods_sku($goods['goods_id'], 1);
            $goods['goods_attr'] = json_decode($goods['goods_attr'], true);

            if ($goods['goods_attr'] && count($goods['goods_attr']['attr_val']) > 1) {
                $_attr = array();
                $html = '<table><tr>';
                foreach ($goods['goods_attr']['attr_title'] as $attr_title) {
                    $html .= '<td>' . $attr_title . '</td>';
                }
                $html .= '</tr>';

                foreach ($goods['goods_attr']['attr_val'] as $k => $_row) {
                    foreach ($_row as $_k => $_row_attr) {
                        $_attr[$_k][$k] = $_row_attr;
                    }
                }

                foreach ($_attr as $k_val => $_attr_val) {
                    $html .= '<tr>';
                    foreach ($_attr_val as $k_title => $attr_title) {
                        if ($k_title == 0) {
                            $html .= '<td>' . $attr_title . '</td>';
                        } else {
                            $attr_val = explode('-', $attr_title);

                            $a1 = sprintf('%.1f', $attr_val[0] / 2.54);
                            $a2 = $attr_val[1] ? ("-" . sprintf('%.1f', $attr_val[1] / 2.54)) : '';
                            $html .= '<td>' . $a1 . $a2 . '(' . $attr_title . 'cm)' . '</td>';
                        }
                    }
                    $html .= '</tr>';
                }
                $html .= '</table>';
            }
            $data = array(
                'goods_id' => $goods['goods_id'],
                'goods_sn' => $siteweb['ab_name'] . $goods['goods_sn'],
                'web_id' => 0,
                'goods_name' => $goods['goods_name'],
                'cat_id' => $goods['cat_id'],
                'shop_price' => $goods['shop_price'],
                'market_price' => $goods['market_price'],
                'body_html' => $goods['body_html'] . $html,
                'is_sales' => 1,
                'variants' => array(),
                'images' => $goods['images'],
                //'tags' => $this->tagslist_model->tags_in($goods['tags'])
            );
            if($this->tagslist_model->tags_in($goods['tags'])){
                $data = array(
                    'tags' => $this->tagslist_model->tags_in($goods['tags']),
                );
            }

            $this->shopify_api->initialize($siteweb);

            $data['shopify_id'] = $siteweb['shopify_id'];
            $data['web_id'] = $siteweb['web_id'];
            $data['goods_sn'] = $siteweb['ab_name'] . $goods['goods_sn'];
            $data['goods_name'] = "{$siteweb['title']} " . $goods['goods_name'];

            $variants = array();
            foreach ($goods_sku as $row) {
                $variants[] = array(
                    'option1' => $row['size'],
                    'option2' => $row['color'],
                    'sku' => $siteweb['ab_name'] . $row['sku_num_sn'],
                    'price' => $goods['shop_price']
                );
            }
            // print_R($variants);die;
            $data['variants'] = $variants;
            $res = $this->db->from('goods')->where(['shopify_id'=>$data['shopify_id']])->get();
            if($res->result_array()){
                $data['cunzai'] = 1;
            }else{
                $data['cunzai'] = 0;
            }
            // print_R($data);die;
            $response = $this->shopify_api->update_up_goods($data);
            // $this->shopify_goods_model->insert_goods($data,$_web['sg_id']);
            $this->db->where(['id'=>$v['id']])->update('goods_upload',['is_upload'=>1]);
        }
    }

  

    //高库龄商品上传
    public function shopify_goods_xin(){
        if($_POST){
        $goods_sn = $this->input->post('goods_sn');
        //查询E仓库存
        $goods_id = $this->db->from('goods')->select('goods_id')->where_in('goods_sn',explode(',',$goods_sn))->get()->result_array();
        foreach ($goods_id as $k => $v) {
            $g_id[] = $v['goods_id'];
        }
        $sku_sn = $this->db->from('goods_sku')->select('sku_sn')->where_in('goods_id',$g_id)->get()->result_array();
        foreach ($sku_sn as $k => $v) {
            $sku[] = $v['sku_sn'];
        }
        $this->load->library('eccang');
        $data['productSku'] = $sku;
        $res = $this->eccang->getProductInventorys($data);
        $this->db->where_in(['goods_id'=>$g_id])->update('goods_sku',['sku_number'=>0]);
        foreach ($res['data'] as $k => $v) {
            if($v['Sellable'] != 0){
                $this->db->where(['sku_sn'=>$v['productSku']])->update('goods_sku',['sku_number'=>$v['Sellable']]);
            }
        }


        $this->load->model('siteweb_model');
        $this->load->model('goods_model');
        $this->load->model('shopify_goods_model');
        $this->load->model('goods_sku_model');
        $this->load->library('sys_message');
        $this->load->model('tagslist_model');
        
        $web_id = 24;
        $siteweb = $this->siteweb_model->siteweb_info(array('web_id' => $web_id));
        
        foreach ($g_id as $key => $goods_id) {


            $goods = $this->goods_model->goods_info($goods_id);
            $goods_sku = $this->db->from('goods_sku')->where(['goods_id'=>$goods_id])->get()->result_array();
            
            foreach ($goods_sku as $k_sku => $v_sku) {
                if($v_sku['sku_number'] == 0){
                    unset($goods_sku[$k_sku]);
                }
            }
            if(empty($goods_sku)){
                continue;
            }
            $variants = array();
            foreach ($goods_sku as $row) {
                $variants[] = array(
                    'option1' => $row['size'],
                    'option2' => $row['color'],
                    'sku' => $siteweb['ab_name'] . $row['sku_num_sn'],
                    'price' => $goods['shop_price'],
                    'inventory_quantity' => $row['sku_number'],
                    "fulfillment_service" => "manual",
                    'inventory_management' => 'shopify'
                );
            }
            
            $data = array(
                'goods_id' => $goods['goods_id'],
                'goods_sn' => $siteweb['ab_name'] . preg_replace("/[a-zA-Z]/","",$goods['goods_sn']),
                'web_id' => $web_id,
                'goods_name' => "{$siteweb['title']} " . $goods['goods_name'],
                'cat_id' => $goods['cat_id'],
                'shop_price' => $goods['shop_price'],
                'market_price' => $goods['market_price'],
                'body_html' => $goods['body_html'],
                'is_sales' => 1,
                'variants' => $variants,
                'tags' => $this->tagslist_model->tags_in($goods['tags'])
            );

            $goods['goods_attr'] = json_decode($goods['goods_attr'], true);

            if ($goods['goods_attr'] && count($goods['goods_attr']['attr_val']) > 1) {
                $html = '<table><tr>';
                foreach ($goods['goods_attr']['attr_title'] as $attr_title) {
                    $html .= '<td>' . $attr_title . '</td>';
                }
                $html .= '</tr>';

                $_attr = array();
                foreach ($goods['goods_attr']['attr_val'] as $k => $_row) {
                    foreach ($_row as $_k => $_row_attr) {
                        $_attr[$_k][$k] = $_row_attr;
                    }
                }

                foreach ($_attr as $k_val => $_attr_val) {
                    $html .= '<tr>';
                    foreach ($_attr_val as $k_title => $attr_title) {
                        if ($k_title == 0) {
                            $html .= '<td>' . $attr_title . '</td>';
                        } else {
                            $attr_val = explode('-', $attr_title);

                            $a1 = sprintf('%.1f', $attr_val[0] / 2.54);
                            $a2 = $attr_val[1] ? ("-" . sprintf('%.1f', $attr_val[1] / 2.54)) : '';
                            $html .= '<td>' . $a1 . $a2 . '(' . $attr_title . 'cm)' . '</td>';
                        }
                    }
                    $html .= '</tr>';
                }
                $html .= '</table>';
                $data['body_html'] .= $html;
            }
            //查询是否上传过
            $shopify_goods = $this->db->from('shopify_goods')
                ->select('sg_id,shopify_id')
                ->where(['goods_id'=>$goods_id,'web_id'=>$web_id])
                ->get()->row_array();
            //存在就修改shopify
            if(!empty($shopify_goods)){
                $arr = ['variants'=>serialize($variants),'update_time'=>time()];
                $this->db->where(['sg_id'=>$shopify_goods['sg_id']])->update('shopify_goods',$arr);
                $this->load->library('shopify_api', $siteweb);
                $response = $this->shopify_api->published_up_sku($shopify_goods['shopify_id'],$variants);
                //如果shopify 找不到从新上传
                if($response['errors'] == 'Not Found'){
                    $shopify_data = $this->shopify_goods_model->up_shopify_data($shopify_goods['sg_id']);
                    $this->load->library('shopify_api', $siteweb);
                    $response = $this->shopify_api->upload_goods($shopify_data);
                    if ($response) {
                        $shopify_goods_data = array(
                            'shopify_id' => $response['id'],
                            'update_time' => 0
                        );
                        $this->shopify_goods_model->update_goods($shopify_goods['sg_id'], $shopify_goods_data);
                    }
                }
            }else{
                //上传shopify
                $sg_id = $this->shopify_goods_model->insert_goods($data);
                if ($sg_id) {
                    $shopify_data = $this->shopify_goods_model->up_shopify_data($sg_id);
                    $this->load->library('shopify_api', $siteweb);
                    $response = $this->shopify_api->upload_goods($shopify_data);
                    if ($response) {
                        $shopify_goods_data = array(
                            'shopify_id' => $response['id'],
                            'update_time' => 0
                        );
                        $this->shopify_goods_model->update_goods($sg_id, $shopify_goods_data);
                    }
                } 
            }
        }
        $this->load->library('sys_message');
        $this->sys_message->success('成功!');
        }else{
            $this->load->view('shopify_goods_xin');
        }
    }

    //批量删除
    public function delete_all(){

        if($_POST){
            $this->load->library('sys_message');
            $this->load->model('siteweb_model');
            $web_id = $this->input->post('web_id');
            $siteweb = $this->siteweb_model->siteweb_info(array('web_id' => $web_id[0]));
            $this->load->library('shopify_api', $siteweb);
            $goods_sn = $this->input->post('goods_sn');
            $goods_sn = explode(',',$goods_sn);
            if(!$web_id || !$goods_sn){
                $this->sys_message->wrong('货号站点不能为空!');
            }
            $goods_id = $this->db->from('goods')->select('goods_id')->where_in('goods_sn',$goods_sn)->get()->result_array();
            foreach ($goods_id as $key => $value) {
                $shopify_id = $this->db->from('shopify_goods')
                    ->select('shopify_id')
                    ->where(['goods_id'=>$value['goods_id'],'web_id'=>$web_id[0]])
                    ->get()->row_array();
                $arr = $this->shopify_api->goods_delete($shopify_id['shopify_id']);
                $this->db->set('is_sales', 0);
                $this->db->where(['goods_id'=>$value['goods_id'],'web_id'=>$web_id[0]]);
                $this->db->update('shopify_goods');
            }
            $this->sys_message->success('成功!');
        }
        $query = $this->db->from('siteweb')->where(['is_delete'=>0])->get();
        $res = $query->result_array();
        $data = array(
            'res' => $res,
        );
        $this->load->view('delete_all',$data);
    }
    
    public function modifySkuRelation(){
        if($_POST){
            $sku = $this->input->post('sku');
            $sku = explode(',',$sku);
            foreach ($sku as $k => $v) {
                if(strlen($v) == 14){
                    $e_sku = substr($v,2);
                    $sku_data = array(
                        'product_sku' => $v,
                        'pcr'         => array(
                            array(
                                'pcr_product_sku' => $e_sku,
                                'pcr_quantity'    => 1,
                            ),
                        ),
                    );
                }else{
                    $sku_sn = $this->db->from('goods_sku')->where(['sku_num_sn'=>substr($v,2)])->select('sku_sn')->get()->row_array();
                    $sku_data = array(
                        'product_sku' => $v,
                        'pcr'         => array(
                            array(
                                'pcr_product_sku' => $sku_sn['sku_sn'],
                                'pcr_quantity'    => 1,
                            ),
                        ),
                    );
                }
                $data['data'][] = $sku_data;
            }
            $this->load->library('eccang');
            $this->eccang->modifySkuRelation($data);
            $this->load->library('sys_message');
            $this->sys_message->success('成功!');
        }
        $this->load->view('modifySkuRelation',$data);
    }
}
